const jQuery = require('jquery');
const path = require('path');
const epointajax = require(path.join(__dirname, '../../../js/epointajax'));
const common = require(path.join(__dirname, '../../../js/common'));
const {
  allconfig
} = require(path.join(__dirname, '../../../js/config'));
const {
  ipcRenderer
} = require('electron');


var pagevue;
jQuery(function ($) {

  var appguid = common.UrlParm.parm('appguid');

  //获取应用基本信息
  epointajax.post(allconfig.serverurl + '/rest/toolsrest/appinfo_detail', {
    appguid: appguid
  }).then((retinfo) => {
    var appinfo = retinfo.custom;
    initPage(appinfo);
  }).catch(msg => {
    console.log(msg);
  });

  function initPage(appinfo) {
    //渲染列表
    pagevue = new Vue({
      el: '#app',
      data: function () {
        return {
          form: {
            appguid: appinfo.appguid,
            iconurl: appinfo.iconurl,
            appname: appinfo.appname,
            appurl: appinfo.appurl,
            operateusername: appinfo.operateusername,
            remark: appinfo.remark,
            collect: appinfo.collect,
            public: appinfo.public,
            enablenode:appinfo.enablenode
          },
          fileList: [{
            name: '',
            url: appinfo.iconurl
          }],
          iconchange: false
        }
      },
      methods: {
        closepage() {
          ipcRenderer.send('pageMyappinfo');
          ipcRenderer.send('initAllApp');
          window.close();
        },
        appinfoedit() {
          epointajax.post(allconfig.serverurl + '/rest/toolsrest/appinfo_update', {
            appguid: this.form.appguid,
            appname: this.form.appname,
            appurl: this.form.appurl,
            remark: this.form.remark,
            public: this.form.public,
            enablenode:this.form.enablenode
          }).then(retinfo => {
            if (this.iconchange) {
              let fileobj = this.fileList[0].raw;
              return epointajax.postfile(allconfig.serverurl + '/rest/toolsrest/appinfo_iconupload', {
                appguid: this.form.appguid
              }, fileobj);
            } else {
              return Promise.resolve(retinfo);
            }
          }).then((retinfo) => {
            if (retinfo.status) {
              this.$message({
                message: retinfo.text,
                type: 'success',
                duration: 1500,
                onClose: function () {
                  this.pagevue.closepage();
                }
              });
            }
          }).catch(msg => {
            console.log(msg);
          });




        },
        handleRemove(file) {
          this.fileList = this.fileList.filter(fileobj => fileobj.uid !== file.uid);
        },
        handleChange(file, fileList) {
          this.iconchange = true;
          file.name = '';
          this.fileList = fileList.slice(-3);
        }
      }
    });
  }


});
